package com.hmall.order.builder;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.hmall.common.client.ItemClient;
import com.hmall.common.client.UserClient;
import com.hmall.common.dto.Address;
import com.hmall.common.dto.Item;
import com.hmall.common.utils.BaseContext;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.OrderParams;
import com.hmall.order.service.impl.OrderDetailService;
import com.hmall.order.service.impl.OrderLogisticsService;
import lombok.Data;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

@Data
public class AllOrderDataBuilder {
    private AllOrderData allOrderData;

    private ItemClient itemClient;

    private UserClient userClient;

    private OrderDetailService orderDetailService;

    private OrderLogisticsService orderLogisticsService;

    private RabbitTemplate rabbitTemplate;
    private OrderParams params;
    Long userId = BaseContext.getCurrentThread();
    long orderId = IdWorker.getId();//生成的订单Id

    public AllOrderDataBuilder(
            ItemClient itemClient,
            UserClient userClient,
            OrderDetailService orderDetailService,
            OrderLogisticsService orderLogisticsService,
            RabbitTemplate rabbitTemplate,
            OrderParams params) {
        this.itemClient = itemClient;
        this.userClient = userClient;
        this.orderDetailService = orderDetailService;
        this.orderLogisticsService = orderLogisticsService;
        this.params = params;
        this.rabbitTemplate = rabbitTemplate;
        allOrderData=new AllOrderData();
    }

    //订单
    public AllOrderData build() {
        return allOrderData;
    }

    public AllOrderDataBuilder buildOrder() {
        Order order = new Order();
        Item item = itemClient.queryItemById(params.getItemId());
        Integer num = params.getNum();
        Integer paymentType = params.getPaymentType();//支付方式

        Long totalFee = item.getPrice() * num;//总金额

        order.setId(orderId);
        order.setTotalFee(totalFee);
        order.setPaymentType(paymentType);
        order.setUserId(userId);
        order.setStatus(1);
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        allOrderData.setOrder(order);
        return this;
    }

    public AllOrderDataBuilder buildOrderDetails() {
        Item item = itemClient.queryItemById(params.getItemId());
        Integer num = params.getNum();
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setId(IdWorker.getId());
        orderDetail.setOrderId(orderId);
        orderDetail.setItemId(item.getId());
        orderDetail.setNum(num);
        orderDetail.setName(item.getName());
        orderDetail.setSpec(item.getSpec());
        orderDetail.setPrice(item.getPrice());
        orderDetail.setImage(item.getImage());
        orderDetail.setCreateTime(new Date());
        orderDetail.setUpdateTime(new Date());
        allOrderData.setOrderDetail(orderDetail);
        return this;
    }

    public AllOrderDataBuilder buildOrderLogistics() {
        OrderLogistics orderLogistics = new OrderLogistics();
        orderLogistics.setOrderId(orderId);

        Address address = userClient.findAddressById(params.getAddressId());

        orderLogistics.setContact(address.getContact());
        orderLogistics.setPhone(address.getMobile());
        orderLogistics.setProvince(address.getProvince());
        orderLogistics.setCity(address.getCity());
        orderLogistics.setTown(address.getTown());
        orderLogistics.setStreet(address.getStreet());
        orderLogistics.setCreateTime(new Date());
        orderLogistics.setUpdateTime(new Date());
        allOrderData.setOrderLogistics(orderLogistics);
        return this;
    }
}
